{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.定义"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 定义一个空集合\n",
    "# 区别于 my_set={}这是空字典的定义\n",
    "demo_set = set()\n",
    "\n",
    "# 集合是无序的->不支持下表索引\n",
    "# 集合是可变的数据类型\n",
    "# 集合中元素是唯一的\n",
    "# 集合一般用于元组或者列表中的元素去重"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.集合的常见操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.1 添加元素"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', ('orange', 'happy'), 'apple', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "# add 作为一个整体添加到集合中,只能接收一个变量且不可变数据类型\n",
    "fruits = {\"apple\",\"banana\",\"cherry\"}\n",
    "fruits.add((\"orange\",\"happy\"))\n",
    "print(fruits)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'cherry', 'orange', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "# update 是把要传入的元素拆分，作为个体传入到集合中\n",
    "# 当参数为字典时，只会将键放入集合\n",
    "x = {\"apple\",\"banana\",\"cherry\"}\n",
    "y = {\"apple\":18,\"banana\":10,\"orange\":20}\n",
    "x.update(y)\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.2 删除元素"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'cherry', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "# remove 删除指定元素\n",
    "x.remove(\"apple\")\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "set.pop() takes no arguments (1 given)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Input \u001B[1;32mIn [18]\u001B[0m, in \u001B[0;36m<cell line: 2>\u001B[1;34m()\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# pop 随机删除集合中的元素\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m \u001B[43mx\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpop\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43morange\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(x)\n",
      "\u001B[1;31mTypeError\u001B[0m: set.pop() takes no arguments (1 given)"
     ]
    }
   ],
   "source": [
    "# pop 随机删除集合中的元素\n",
    "x.pop()\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "# discard 不存在不会报错\n",
    "x.discard(\"banana\")\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.去重"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 6, 7}\n"
     ]
    },
    {
     "data": {
      "text/plain": "[1, 2, 3, 4, 6, 7]"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将元组或列表转换为集合\n",
    "del list\n",
    "my_list=[1,1,2,3,4,6,6,7]\n",
    "my_set=set(my_list)\n",
    "print(my_set)\n",
    "list(my_set)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.4.差集"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry'}\n",
      "{'cherry'}\n"
     ]
    }
   ],
   "source": [
    "x = {\"apple\",\"banana\",\"cherry\"}\n",
    "y = {\"apple\",\"banana\",\"orange\"}\n",
    "# x-y\n",
    "print(x-y)\n",
    "\n",
    "# difference\n",
    "print(x.difference(y))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry'}\n"
     ]
    }
   ],
   "source": [
    "# difference_update() 移除集合中的元素，与difference的区别在于，它是在原有的set上进行移除\n",
    "x.difference_update(y)\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.5.对称差集"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'orange'}\n",
      "{'cherry', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "# symmetric_difference() 返回两个集合中不重复的元素集合\n",
    "x = {\"apple\",\"banana\",\"cherry\"}\n",
    "y = {\"apple\",\"banana\",\"orange\"}\n",
    "print(x.symmetric_difference(y))\n",
    "\n",
    "# ^\n",
    "print(x^y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cherry', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "# symmetric_difference_update() 移除相同的，添加不同的\n",
    "x.symmetric_difference_update(y)\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.6 交集"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3}\n",
      "{3}\n"
     ]
    }
   ],
   "source": [
    "# intersection\n",
    "x= {1,2,3}\n",
    "y= {3,4,5}\n",
    "z ={1,3,5}\n",
    "result =x.intersection(y,z)\n",
    "print(result)\n",
    "\n",
    "print(x&y&z)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3}\n"
     ]
    }
   ],
   "source": [
    "# intersection_update 类似于symmetric_difference_update\n",
    "x.intersection_update(y,z)\n",
    "print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.7 并集"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n",
      "{1, 2, 3, 4, 5}\n"
     ]
    }
   ],
   "source": [
    "x= {1,2,3}\n",
    "y= {3,4,5}\n",
    "x.update(y)\n",
    "print(x)\n",
    "\n",
    "print(x|y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.8 判断"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# isdisjoint 判断是否包含相同元素\n",
    "x.isdisjoint(y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.9 查询"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x= {1,2,3}\n",
    "y= {3,4,5}\n",
    "\n",
    "3 in y"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}